# -*- coding:utf-8 -*-
from rest_framework import serializers
from book import models


# 批量修改：写一个类，继承ListSerializer，重写update方法
class BookListSerializer(serializers.ListSerializer):
    def update(self, instance, validated_data):
        data = []
        for i, si_data in enumerate(validated_data):
            ret = self.child.update(instance[i], si_data)
            data.append(ret)
        return data


# 序列化的是数据库的表，尽量用ModelSerializer
class BookModelSerializer(serializers.ModelSerializer):
    class Meta:
        list_serializer_class = BookListSerializer
        model = models.Book
        fields = ('id', 'title', 'price', 'authors', 'publish', 'publish_name', 'author_list')
        extra_kwargs = {
            'publish': {'write_only': True},
            'authors': {'write_only': True},
            'publish_name': {'read_only': True},
            'author_list': {'read_only': True},
        }
